﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using Utils;
using BLL;
using System.Text;
using System.Collections.Specialized;
using DataContext;

public partial class Pages_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            
            var typeSys = (Utils.CookieHelper.GetCookie("typeSystemHiloApp") ?? "1").Trim();
            try
            {
                ddlServiceID.SelectedValue = typeSys;
            }
            catch {                
                ddlServiceID.SelectedIndex = 0;
            }
        }
    }

    protected void lbtLogin_Click(object sender, EventArgs e)
    {
        string userName = txtUserName.Text;
        string pass = txtPass.Text;
        int serviceID = Convert.ToInt32(ddlServiceID.SelectedValue);
        CtrAdmin ctrA = new CtrAdmin();        
        CookieHelper.SetCookie("typeSystemHiloApp", serviceID.ToString());
        CtrAdmin ctrAdmin = new CtrAdmin();
        bool result = ctrAdmin.CheckLogin(userName, pass);
        if (userName != "admin")
        {
            
            if (!ctrAdmin.CheckExistsAdminByUserName(userName, serviceID))
            {
                lblRespose.Text = "Tài khoản này không được phép đăng nhập vào hệ thống hoặc không có quyền trên dịch vụ này!";
                divMess.Visible = true;
                return;
            }
            
            if (result)
            {
                LoginBackendAdmin(userName, serviceID, false);
            }
            else
            {
                lblRespose.Text = "Mật khẩu chưa chính xác";
                divMess.Visible = true;
                return;
            }
        }
        else
        {
            if (result)
            {
                LoginBackendAdmin(userName, serviceID, true);
            }
            else
            {
                lblRespose.Text = "Mật khẩu chưa chính xác";
                divMess.Visible = true;
                return;
            }

        }
            
    }

    public void LoginBackendAdmin(string userName, int serviceID, bool isRoot)
    {
        CtrAdmin ctrAdmin = new CtrAdmin();
        StringBuilder permission = new StringBuilder();
        permission.Append('|');
        HybridDictionary hbFunction = new HybridDictionary();
        CtrFunction ctrFunction = new CtrFunction();
        int _adminID = ctrAdmin.GetInfoAdminByUserName(userName).Id;
        int permissionItem;
        if (isRoot)
        {
            var tp = ctrFunction.GetListFunctionByAdminRootForLogin(serviceID);
            foreach (uspFunctionGetListByAdminRootForLoginResult Function in tp)
            {
                if (hbFunction[Function.Url] == null)
                {
                    hbFunction.Add(Function.Url, Function);
                }

            }
            uspFunctionGetListByAdminRootForLoginResult functionInfo;
            foreach (string key in hbFunction.Keys)
            {
                functionInfo = hbFunction[key] as uspFunctionGetListByAdminRootForLoginResult;
                permission.Append(functionInfo.Url).Append("#").Append("15").Append("|");
            }
        }
        else
        {
            uspFunctionGetListByAdminIdForLoginResult functionInfo;
            var tp = ctrFunction.GetListFunctionByAdminIDForLogin(_adminID, serviceID);
            foreach (uspFunctionGetListByAdminIdForLoginResult Function in tp)
            {
                if (hbFunction[Function.Url] == null)
                {
                    hbFunction.Add(Function.Url, Function);
                }
                else
                {
                    functionInfo = hbFunction[Function.Url] as uspFunctionGetListByAdminIdForLoginResult;
                    permissionItem = functionInfo.Permission | Function.Permission??0;
                    functionInfo.Permission = permissionItem;
                }
            }
            foreach (string key in hbFunction.Keys)
            {
                functionInfo = hbFunction[key] as uspFunctionGetListByAdminIdForLoginResult;
                permission.Append(functionInfo.Url).Append("#").Append(functionInfo.Permission).Append("|");
            }
        }

        CtrAdmin.Login(userName, permission.ToString(), serviceID);
        Response.Redirect("/index.htm");
        
    }
}